from django.shortcuts import render

# Create your views here.
from django.views import View

from .models import User

from django.http import JsonResponse


class UsernameCountView(View):

    def get(self, request, username):
        '''接收用户名，判断是否重复注册'''

        # 进入数据库查询username在数据库中的个数
        try:
            count = User.objects.filter(username=username).count()

        except Exception as e:

            return JsonResponse({'code':400,
                                 'errmsg':'查询数据库失败'})

        # 拼接json字符串，返回给前端
        return JsonResponse({'code': 400,
                             'errmsg': 'ok',
                             'count':count})



class MobileCountView(View):

    def get(self, request, mobile):
        '''判断手机号是否重复注册'''
        # 1.查询mobile在mysql中的个数
        try:
            count = User.objects.filter(mobile=mobile).count()
        except Exception as e:
            return JsonResponse({'code':400,
                                 'errmsg':'查询数据库出错'})

        # 2.返回结果(json)
        return JsonResponse({'code':0,
                             'errmsg':'ok',
                             'count':count})